package com.boarsoft.boar.soagov.flow.biz;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.entity.FlowInfo;
import com.boarsoft.boar.entity.SvcInfo;
import com.boarsoft.boar.soagov.flow.FlowBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("flowBiz")
public class FlowBizImpl extends SimpleBizImpl implements FlowBiz {
	@Override
	@Transactional
	public void delete(String id) {
		dao.delete(FlowInfo.class, id);
	}

	@Override
	@Transactional
	public void enable(String id) {
		FlowInfo o = (FlowInfo) dao.get(FlowInfo.class, id);
		o.setStatus(FlowInfo.STATUS_INUSE);
	}

	@Override
	@Transactional
	public void disable(String id) {
		FlowInfo o = (FlowInfo) dao.get(FlowInfo.class, id);
		o.setStatus(FlowInfo.STATUS_NOUSE);
	}

	@Override
	@Transactional
	public void save(FlowInfo f) {
		dao.merge(f);
	}

	@Override
	@Transactional(readOnly = true)
	public FlowInfo get(String id) {
		return (FlowInfo) dao.get(FlowInfo.class, id);
	}

	@Override
	@Transactional(readOnly = true)
	public PagedResult<FlowInfo> list(String projId, String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append("select s from ").append(SvcInfo.class.getName());
		sb.append(" s where 1=1");
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (s.code like '%").append(key);
			sb.append("%' or s.name like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal(sb.toString());
		List<FlowInfo> lt = dao.list(sb.toString(), pageNo, pageSize);
		return new PagedResult<FlowInfo>(total, lt, pageNo, pageSize);
	}

	/**
	 * 用于查询最近编辑的服务
	 */
	@Override
	@Transactional(readOnly = true)
	public PagedResult<FlowInfo> find(int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" and catalog = 'soagov'");
		sb.append(" and url like '%edit%'");
		int total = dao.getTotal(FlowInfo.class, sb.toString());
		sb.append(" order by id");
		List<FlowInfo> lt = dao.list(FlowInfo.class, sb.toString(), pageNo, pageSize);
		return new PagedResult<FlowInfo>(total, lt, pageNo, pageSize);
	}
}